package torres.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.LinkedHashSet;

import javax.swing.JPanel;

import torres.ability.Ability;
import torres.entity.Entity;
import torres.entity.Skill;
import torres.map.World;

public class AbilityMenuView extends JPanel {

	int screenWidth ,x, imageWidth;
	int screenHeight, y, imageHeight;
	Entity player;
	
	public AbilityMenuView() {

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		screenWidth = screenSize.width;
		screenHeight = screenSize.height;
		//System.out.println("Screen Width: " + screenWidth + " Screen Height: " + screenHeight);
		
		x = 2*screenWidth/3;
		y = 2*screenHeight/3;
		
		setIgnoreRepaint(true);
		setPreferredSize(new Dimension(x,y));
		setLocation(screenWidth-x/2, screenHeight-y/2);
		setLayout(null);
		setVisible(true);
		setFocusable(true);
		setOpaque(true);
		
		player = World.getInstance().getPlayer();

	}
	
	public synchronized void paintComponent(Graphics g) {
		super.paintComponent(g);
		LinkedHashSet<String> skillPointList = new LinkedHashSet<String>();
		
		Graphics2D g2 = (Graphics2D)g;
		
		g.setColor(Color.black);
		drawWindow(g);

		int yOffset = 30;
		int xOffset = 20;
		
		Font body = new Font("Helvetica",Font.BOLD, 14);
		Font headers = new Font("Helvetica",Font.ITALIC, 18);
		g.setFont(headers);
		
		//Draw Left
		g.setColor(Color.yellow); //changed to see menu better
		//g.setColor(Color.white);
		g.drawString("Stats",screenWidth/2-x/2 + xOffset,screenHeight/2-y/2 + yOffset );
		g.drawString("Skill Points:\t\t\t\t"+player.getSkillPoints(), screenWidth/2 - x/2 + xOffset, screenHeight/2 + yOffset);
		
		//Draw right
		//g.setColor(Color.black);
		//g.setColor(Color.yellow);
		g.drawString("Abilities", screenWidth/2-x/2+imageWidth/2 + xOffset, screenHeight/2-y/2 + yOffset);
		//g.drawString("Description",screenWidth/2-x/2+imageWidth/2 + xOffset, screenHeight/2 + yOffset);
		
		drawPlayerImage(g2);
		
		//Draw Left
		g.setFont(body);
		//g.setColor(Color.yellow);
		drawStats(g2);
		drawSkillPoints(g2, player.retreiveSkillList(skillPointList));
		
		//Draw Right
		//g.setColor(Color.black);
		drawAbilityList(g2, player.getOccupation().getAbilityList(), player.getOccupation().getAttackList());
		//this.returnRectangleAreas(g2);
		//drawSkillDescription(g2, Skill.BINDWOUNDS.getDescription());
		//System.out.println("Finished painting");
		
	}

	
	
	/**
	 * @param g2
	 * @param abilityList
	 * @param attackList
	 */
	private void drawAbilityList(Graphics2D g2, LinkedHashSet<Ability> abilityList, LinkedHashSet<Ability> attackList) {
		// TODO Auto-generated method stub
		int i = 0;
		int xOffset = 20; 
		int yOffset = 30;
		g2.setFont(new Font("Helvetica",Font.BOLD, 20));
		//g2.drawRect(screenWidth/2-x/2+imageWidth/2 + xOffset+20,
			//	screenHeight/2-y*7/16+10, 300, 30*8);
		
		for(Ability abl : abilityList) {
			g2.drawString(abl.getName(), screenWidth/2-x/2+imageWidth/2 + xOffset+20, screenHeight/2-y*7/16 + yOffset*i +30);
			//g2.drawRect(screenWidth/2-x/2+imageWidth/2 + xOffset+20, screenHeight/2-y*3/8 + yOffset*i-30, 100, 30);
			int  j = 1;
			for (Ability lba : attackList) {
				if (attackList.contains(abl) && abl.getName().equals(lba.getName())) {
					g2.drawString("Attack " + j, screenWidth/2-x/2+imageWidth/2 + xOffset+20+300, screenHeight/2-y*3/8 + yOffset*i);
				}
				j++;
			}
				
			//System.out.println("AbilityMenuView.drawAbilityList(): skill: " + abl.toString());
			//System.out.println("SkillPointView.drawSkillPoints(): Xinit: " + (screenWidth/2 + imageHeight/2 + 30 )+ " Yinit: " + (screenHeight/2 - y/2 + 30*i));
			i++;
		}
		for (Ability lba : attackList) {
			System.out.println("Order of Attacks: " + attackList.toString());
		}
		
	}
	
	/**
	 * @param hashRect
	 * @param abilityList
	 * @param attackList
	 * @return
	 */
	public HashMap<Ability, Rectangle> returnRectangleAreas() {
		HashMap<Ability, Rectangle> hashRect = new HashMap<Ability, Rectangle>();
		LinkedHashSet<Ability> abilityList = World.getInstance().getPlayer().getOccupation().getAbilityList();
		int i = 0;
		int xOffset = 20; 
		int yOffset = 30;
		//g2.setFont(new Font("Helvetica",Font.BOLD, 20));
		for(Ability abl : abilityList) {
			//g2.drawString(abl.getName(), screenWidth/2-x/2+imageWidth/2 + xOffset+20, screenHeight/2-y*3/8 + yOffset*i);
			hashRect.put(abl, new Rectangle(screenWidth/2-x/2+imageWidth/2 + xOffset+20, screenHeight/2-y*3/8 + yOffset*i-30, 100, 30));
			//g2.drawRect(screenWidth/2-x/2+imageWidth/2 + xOffset+20, screenHeight/2-y*3/8 + yOffset*i-30, 100, 30);
			
			
			//for (int j = 0; j < 3; j++) 
				//if (attackList.get(j) == abl)
					//g2.drawString("Attack" + j, screenWidth/2-x/2+imageWidth/2 + xOffset+20+100, screenHeight/2-y*3/8 + yOffset*i);		
			//System.out.println("SkillPointView.drawSkillPoints(): skill: " + abl.toString());
			//System.out.println("SkillPointView.drawSkillPoints(): Xinit: " + (screenWidth/2 + imageHeight/2 + 30 )+ " Yinit: " + (screenHeight/2 - y/2 + 30*i));
			i++;
		}
		return hashRect;
	}

	public void drawWindow(Graphics g) {
		ImageLoader loader = new ImageLoader("texture.jpg");
		BufferedImage img = loader.getImage();
		imageWidth = img.getWidth();
		imageHeight = img.getHeight();
		//System.out.println("SkillPointView.drawWindow(): ImageWidth: " + imageWidth + " imageHeight: " + imageHeight);
		
		g.drawImage(img, screenWidth/2-x/2,screenHeight/2-y/2,x,y,null);
		
		/*
		g.draw3DRect(screenWidth/2-x/2,screenHeight/2-y/2, x, y, true);
		g.setColor(Color.darkGray);
		g.fill3DRect(screenWidth/2-x/2,
				screenHeight/2-y/2,
				x,y,
				true);
		*/
	}
	public void drawSkillDescription(Graphics2D g2, String description) {
		int xOffset = 20;
		int yOffset = 50;
		
		g2.drawString(description,screenWidth/2-x/2+imageWidth/2 + xOffset, screenHeight/2 + yOffset);
	}
	public void drawSkillPoints(Graphics2D g2, LinkedHashSet<String> skillList) {
		int yOffset = 30;
		int xOffset = 20;
		int yImageOffset, xImageOffset;
		int i = 0;
		
		ImageLoader loader =  new ImageLoader("skillPointArrow.png");
		BufferedImage img = loader.getImage();
		xImageOffset = img.getHeight();
		yImageOffset = img.getHeight();
		
		//System.out.println("SkillPointView.drawSkillPoints(): Xinit: " + (screenWidth/2 + imageHeight/2 + 30 )+ " Yinit: " + (screenHeight/2 - y/2 + 30*i));
		
		for(String str : skillList) {
			g2.drawString(str,  screenWidth/2 - x/2 + xOffset+20, screenHeight/2 + 50+yOffset*i);
			if (player.getSkillPoints() > 0)
				g2.drawImage(img,  screenWidth/2 - x/2 + 20 + xOffset*12,
						screenHeight/2 +50+ yOffset*i-yImageOffset/2-5,null);
			//System.out.println("SkillPointView.drawSkillPoints(): skill: " + str.toString());
			//System.out.println("SkillPointView.drawSkillPoints(): Xinit: " + (screenWidth/2 + imageHeight/2 + 30 )+ " Yinit: " + (screenHeight/2 - y/2 + 30*i));
			i++;
		}
	}
	
	
	public HashMap<Skill, Point> getSkillHash(LinkedHashSet<Skill> skillList) {
		HashMap<Skill, Point> skillHash = new HashMap<Skill, Point>();
		int yOffset = 30;
		int xOffset = 20;
		int yImageOffset, xImageOffset;
		int i = 0;
		
		ImageLoader loader =  new ImageLoader("skillPointArrow.png");
		BufferedImage img = loader.getImage();
		xImageOffset = img.getHeight();
		yImageOffset = img.getHeight();
		
		for(Skill skill : skillList) {
			//g2.drawString(str,  screenWidth/2 - x/2 + xOffset+20, screenHeight/2 + 50+yOffset*i);
			//if (player.getSkillPoints() > 0)
				//g2.drawImage(img,  screenWidth/2 - x/2 + 20 + xOffset*10, screenHeight/2 +50+ yOffset*i-yImageOffset/2-5,null);
			//System.out.println("SkillPointView.getSkillHash(): skill: " + str.toString());
			//System.out.println("SkillPointView.drawSkillPoints(): Xinit: " + (screenWidth/2 + imageHeight/2 + 30 )+ " Yinit: " + (screenHeight/2 - y/2 + 30*i));
			//i++;
			int j = (screenWidth/2 - x/2 + 20 + xOffset*12)+15;
			int k =  (screenHeight/2 +50+ yOffset*i-yImageOffset/2-5)+15;
			skillHash.put(skill, new Point(j, k));
			i++;
		}
		return skillHash;
		
	}
	
	public int getImageRadius() {
		ImageLoader loader =  new ImageLoader("skillPointArrow.png");
		BufferedImage img = loader.getImage();
		// TODO Auto-generated method stub
		System.out.println("SkillPointView.getImageRadius: " +  img.getHeight()/2);
		return img.getHeight()/2;
	}
	
	public void drawPlayerImage(Graphics2D g2) {
		
		ImageLoader imageLoader = new ImageLoader("llamaE.png");
		BufferedImage playerImage = imageLoader.getImage();
		
		g2.drawImage(playerImage,
				screenWidth/2-x/4-playerImage.getWidth()/2,
				screenHeight/2 -y/4 -playerImage.getHeight()/2,
				null);
	}
	
	
	
	public void drawStats(Graphics2D g2) {

		String name = new String(player.getDisplayName());
		String occupation = new String(player.getOccupation().toString());
		String intellect = new String(Integer.toString(player.getIntellect()));
		String strength = new String(Integer.toString(player.getAgility()));
		String hardiness = new String(Integer.toString(player.getHardiness()));
		String level = new String(Integer.toString(player.getLevel()));
		String experience = new String(Integer.toString(player.getExperience()));
		String offensive = new String(Integer.toString(player.getOffensiveRating()));
		String defensive = new String(Integer.toString(player.getDefensiveRating()));
		String armor = new String(Integer.toString(player.getArmorRating()));
		String movement = new String(Integer.toString(player.getMovement()));
		
		int yOffset = 50;
		g2.drawString("Name: " + name,screenWidth/2-x/2 + 42,screenHeight/2-y/2 +yOffset);
		g2.drawString("Occupation: " + occupation,screenWidth/2-x/2 + 42,screenHeight/2-y/2 +yOffset+20);
		g2.drawString("Intellect: " + intellect,screenWidth/2-x/2 + 42,screenHeight/2-y/2 +yOffset+40);
		g2.drawString("Strength: " + strength,screenWidth/2-x/2 + 42,screenHeight/2-y/2 +yOffset+60);
		g2.drawString("Hardiness: " + hardiness,screenWidth/2-x/2 + 42,screenHeight/2-y/2 +yOffset+80);
		g2.drawString("Level: " + level,screenWidth/2-x/2 + 42,screenHeight/2-y/2 +yOffset+100);
		g2.drawString("Experience: " + experience,screenWidth/2-x/2 + 42,screenHeight/2-y/2 +yOffset+120);
		g2.drawString("Defensive Rating: " + offensive,screenWidth/2-x/2 + 42,screenHeight/2-y/2 +yOffset+140);
		g2.drawString("Offensive Rating: " + defensive,screenWidth/2-x/2 + 42,screenHeight/2-y/2 +yOffset+160);
		g2.drawString("Armor Rating: " + armor,screenWidth/2-x/2 + 42, screenHeight/2-y/2 +yOffset+180);
		g2.drawString("Movement: " + movement,screenWidth/2-x/2 + 42,screenHeight/2-y/2 +yOffset+200);
		
	}
	public void update(Graphics g) {
		paintComponent(g);
	}
	public int getScreenHeight() {
		return this.screenHeight;
	}
	public int getScreenWidth() {
		return this.screenWidth;
	}


}